<template>
  <div class="container">
    <!-- 表单筛选区域 -->
    <el-form inline>
      <el-form-item label="标题">
        <el-input
          v-model="queryData.title"
          size="small"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="queryData.cateid" placeholder="请选择">
          <el-option
            v-for="item in listCate"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="valueTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeTime"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >搜索</el-button>
        <el-button size="small" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作区域 -->
    <el-row type="flex" justify="space-between" align="middle">
      <!-- 左侧 -->
      <div class="left">
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-plus"
          @click="$router.push('/content_article/add')"
        >新增</el-button>
      </div>
      <!-- 右侧 -->
      <div class="right">右侧区域</div>
    </el-row>
    <br>
    <!-- 表格区域 -->
    <el-table :data="list" border>
      <el-table-column type="index" label="#" />
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="catename" label="分类名" />
      <el-table-column prop="pic" label="图片">
        <template slot-scope="{row}">
          <img :src="showImg(row.pic)" style="height:80px" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="create_date" label="创建时间">
        <template slot-scope="{row}">
          {{ timestampToTime(row.create_date) }}
        </template>
      </el-table-column>
      <el-table-column label="是否推荐">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.ishot"
            disabled
            :active-value="1"
            :inactive-value="0"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否置顶">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.istop"
            disabled
            :active-value="1"
            :inactive-value="0"
          />
        </template>
      </el-table-column>
      <el-table-column prop="catename" label="文章状态" />
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            plain
            size="small"
            icon="el-icon-edit"
            @click="editRow(row.id)"
          >修改</el-button>
          <el-button
            type="danger"
            plain
            size="small"
            icon="el-icon-delete"
            @click="delRow(row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <!-- 分页区域 -->
    <el-pagination
      :current-page.sync="queryData.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryData.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalNum"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

  </div>
</template>
<script>
import {
  getyddArticleListApi,
  // getyddArticleInfoApi,
  delyddArticleInfoApi,
  getCateAll
} from '@/api/ydd_article'
// import bus from '@/common/bus'
export default {
  name: 'Article',
  components: {
  },
  data() {
    return {
      valueTime: [],
      listCate: [],
      list: [], // 表格数据
      totalNum: 0, // 总数
      // 列表的需要的参数
      queryData: {
        start_time: '',
        end_time: '',
        title: '',
        cateid: '',
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    // 调用初始化
    this.initData()
  },
  methods: {
    changeTime(val) {
      this.queryData.start_time = this.timeToTimestamp(val[0])
      this.queryData.end_time = this.timeToTimestamp(val[1])
    },
    timestampToTime(timestamp) {
      timestamp = timestamp || null
      const date = new Date(timestamp)// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      const Y = date.getFullYear() + '-'
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
      const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
      const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return Y + M + D + h + m + s
    },
    timeToTimestamp(time) {
      const timestamp = Date.parse(new Date(time).toString())
      // timestamp = timestamp / 1000; //时间戳为13位需除1000，时间戳为13位的话不需除1000
      console.log(time + '的时间戳为：' + timestamp)
      return timestamp
    },
    showImg(img) {
      return process.env.VUE_APP_BASE_API + '/' + img
    },
    // 初始化数据
    async initData() {
      // 发起请求
      const res = await getyddArticleListApi(this.queryData)
      // 数据可能需要加工
      // 赋值
      // console.log(res)
      this.totalNum = res.count
      this.list = res.data
      const res1 = await getCateAll()
      console.log(res1)
      this.listCate = res1
    },
    // 搜索提交
    onSubmit() {
      // 页码变成1
      this.queryData.page = 1
      // 拉取列表
      this.initData()
    },
    // 搜索重置
    onReset() {
      // 重置搜索表单
      this.queryData = {
        advposdesc: '',
        advposname: '',
        page: 1,
        limit: 10
      }
      // 拉取列表
      this.initData()
    },
    // 每页数量变化的时候
    sizeChange(newSize) {
      // newSize 最新的每页数量
      this.queryData.limit = newSize
      this.initData()
    },
    // 页码变化事件
    currentChange(newPage) {
      // newPage 最新的页码
      this.queryData.page = newPage
      this.initData()
    },
    // 修改事件
    async editRow(rowId) {
      // 获取改行的数据
      // const res = await getyddArticleInfoApi({ id: rowId })
      // 赋给弹框里面的表单
      // console.log(res)
      // bus.$emit('Info', res)
      this.$router.push(`/content_article/edit/${rowId}`)
    },
    // 删除事件
    async delRow(rowId) {
      // 弹出确认框
      await this.$confirm('确认删除吗')
      // 发生删除请求
      await delyddArticleInfoApi({ id: rowId })
      // 提示成功
      this.$message.success('删除成功')
      // 边缘问题：如果不是第一页，且这一页只有一个，且删除成功，页码需要-1
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      // 列表更新
      this.initData()
    }
  }
}
</script>
<style scoped>
.container {
  padding: 30px;
}
</style>
